<?php
include_once 'setup/setup.php';
include_once 'class/class.phpmailer.php';
/* Filter input */
$u_name=htmlentities($_POST['u_name'],ENT_QUOTES,"UTF-8");
$u_dep=htmlentities($_POST['u_dep'],ENT_QUOTES,"UTF-8");
#$u_name=filter_input(INPUT_POST, 'u_name',FILTER_SANITIZE_STRING,FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW);
#$u_dep=filter_input(INPUT_POST, 'u_dep',FILTER_SANITIZE_STRING,FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW);
$u_mail = filter_input(INPUT_POST, 'u_mail',FILTER_VALIDATE_EMAIL);
do{
	if(!$u_name){
		$msg="姓名欄是空的！";
		break;
	}
	
	
	if(!$u_mail){
		$msg="電子郵件是空的或無效";
		break;
	}
}while(0);

/* user pdo */

/* generate random string */
$rand_string=generatorPassword();
/* SEND MAIL TO USER */
$mail = new PHPMailer();
$mail->From     = "service@cta.chc.edu.tw";
$mail->FromName = "彰化縣教師會";
$mail->Host     = "localhost";
$mail->Mailer   = "mail";
$mail->IsHTML(true);
$mail->CharSet  = "UTF-8";
$body  = $u_name."您好<br/>";
$body .= "請點選下列的連結已完成訂閱手續<br/>";
$body .="<a href='http://www.cta.chc.edu.tw'target='_blank'>cta@{$rand_string}</a><br/>";
$mail->Subject = "彰化縣教師會電子報訂閱確認信";
$mail->Body = $body;
$mail->AddAddress($u_mail);
$mail->Send();


/* insert data */
$stmt = $dbh->prepare("INSERT INTO e_puser (u_name,u_mail,u_dep,u_teac,u_hasteac,u_check) VALUES (?,?,?,?,?,?)");
$stmt->bindParam(1, $u_name,PDO::PARAM_STR);
$stmt->bindParam(2, $u_mail,PDO::PARAM_STR);
$stmt->bindParam(3, $u_dep,PDO::PARAM_STR);
$stmt->bindParam(4, $_POST['ist'],PDO::PARAM_BOOL);
$stmt->bindParam(5, $_POST['isg'],PDO::PARAM_BOOL);
$stmt->bindParam(6,	$rand_string,PDO::PARAM_STR);
$stmt->execute();
/* release db resource */
//$dbh = null;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.2.0/build/reset-fonts-grids/reset-fonts-grids.css">
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.3.0/build/base/base-min.css">
<title>系統訊息</title>
<style type="text/css">
<!--

-->
</style>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body><div id="man">
<div class="h_line">彰化縣教師會電子報-系統訊息</div>
<?php if(!$msg){ ?>
<ul>
  <li><?php echo $u_name?>您好，您的電子郵件為【<?php echo $u_mail; ?>】，本系統己發出確認信件！</li>
  <li>請至您的電子郵件信箱收取本會發送的確認信並點選確認，謝謝！請勿直接回覆！</li>
  <li>如果有相關問題請來信至 <a href="mailto:service@cta.chc.edu.tw">service@cta.chc.edu.tw </a></li>
</ul>
<?php }else{ //if end ?>
<ul><li><?php echo $msg; ?><br /><input  value="回上頁重填" name="backto" type="button" onClick="location.href='register.php'" />
</li></ul>
<?php } ?>
</div>
</body>

</html>
